using System;
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using XCZProject;

namespace QFramework.Example
{
	public class UIGamePanelData : UIPanelData
	{
	}
	public partial class UIGamePanel : UIPanel
	{
		// 当炸弹爆炸时的震屏效果事件
		public static EasyEvent flashScreen = new EasyEvent();

		// 宝箱界面事件
		public static EasyEvent OpenTreasurePanelEvent = new EasyEvent();
		
		protected override void OnInit(IUIData uiData = null)
		{
			mData = uiData as UIGamePanelData ?? new UIGamePanelData();
			// please add init code here

			Time.timeScale = 1.0f;
			EnemyGenerator.EnemyCount.RegisterWithInitValue(enemyCount =>
			{
				//print(enemyCount);
				EnemyCountText.text = "敌人:" + enemyCount;
			}).UnRegisterWhenGameObjectDestroyed(gameObject);

			Global.CurrentSecond.RegisterWithInitValue(currentSeconds =>
			{
				// 让事件会在每30帧更新一次，避免每帧更新影响性能
				if (Time.frameCount%30==0)
				{
					var currentSecondsInt = Mathf.FloorToInt(currentSeconds);
					var seconds = currentSecondsInt % 60;
					var minute = currentSecondsInt / 60;
					//print(TimeText.name);
					TimeText.text = "时间:" + $"{minute:00}:{seconds:00}";
				}
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			
			// 经验值改变事件，单独的register和这个的区别是，前者是注册事件，后者是注册事件后立即执行一次
			Global.Exp.RegisterWithInitValue(exp =>
			{
				//ExpText.text = "经验值：" + exp + "/" + Global.ExpToNextLevel();
				ExpValue.fillAmount = exp / (float)Global.ExpToNextLevel();
			}).UnRegisterWhenGameObjectDestroyed(gameObject);

			// 等级值改变事件
			Global.Lv.RegisterWithInitValue(lv =>
			{
				LevelText.text = "等级：" + lv;
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			
			//Global.Coin.Value = PlayerPrefs.GetInt("coin",0);
			// 金币改变事件
			Global.Coin.RegisterWithInitValue(coin =>
			{
				CoinText.text = "金币：" + coin;
				//PlayerPrefs.SetInt(nameof(coin),coin);
			}).UnRegisterWhenGameObjectDestroyed(gameObject);

			Global.Lv.Register(lv =>
			{
				//Debug.Log("升级啦1");
				// 暂停游戏，为什么不在上面RegisterWithInitValue里面注册呢
				// 因为上面注册方法是开始初始化时会调用一次，如果放上面，则开始游戏就暂停了
				Time.timeScale = 0;
				//Debug.Log(ExpUpgradePanel.name);
				ExpUpgradePanel.Show();
				AudioKit.PlaySound("level_up");
			});

			ExpUpgradePanel.Hide();
			Global.Exp.RegisterWithInitValue(exp =>
			{
				if (exp >= Global.ExpToNextLevel())
				{
					Global.Exp.Value -= Global.ExpToNextLevel();
					Global.Lv.Value++;
				}
			}).UnRegisterWhenGameObjectDestroyed(gameObject);


			var enemyGenerator=FindObjectOfType<EnemyGenerator>();
			ActionKit.OnUpdate.Register(() =>
			{
				Global.CurrentSecond.Value += Time.deltaTime;
				// 获取场景内的所有敌人物体（不包含active为false的）数量
				// bool isHasTrueEnemy = FindObjectOfType<Enemy>(false);

				if (enemyGenerator.LastWave && enemyGenerator.CurrentWave==null
				    && EnemyGenerator.EnemyCount.Value==0)
				{
					ActionKit.Delay(1, () =>
					{
						this.CloseSelf();
						UIKit.OpenPanel<UIGamePassPanel>();
					}).Start(this);
				}
			}).UnRegisterWhenGameObjectDestroyed(gameObject);

			flashScreen.Register(() =>
			{
				// 震屏效果
				ActionKit
					.Sequence()
					.Lerp(0, 0.5f, 0.1f, alpha =>
					{
						ScreenColor.ColorAlpha(alpha);
					})
					.Lerp(0.5f, 0, 0.2f, alpha =>
					{
						ScreenColor.ColorAlpha(alpha);
					}, () => ScreenColor.ColorAlpha(0))
					.Start(this);
			}).UnRegisterWhenGameObjectDestroyed(gameObject);

			OpenTreasurePanelEvent.Register(() =>
			{
				Time.timeScale = 0.0f;
				TreasureChestPanel.Show();
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
		}
		
		protected override void OnOpen(IUIData uiData = null)
		{
		}
		
		protected override void OnShow()
		{
		}
		
		protected override void OnHide()
		{
		}
		
		protected override void OnClose()
		{
		}
	}
}
